Imports <xmlns:nc="http://niem.gov/niem/niem-core/2.0">
Imports <xmlns:lexs="http://usdoj.gov/leisp/lexs/3.1">
Imports <xmlns:lexspd="http://usdoj.gov/leisp/lexs/publishdiscover/3.1">
Imports <xmlns:lexsdigest="http://usdoj.gov/leisp/lexs/digest/3.1">
Imports <xmlns:s="http://niem.gov/niem/structures/2.0">
Imports <xmlns:j="http://niem.gov/niem/domains/jxdm/4.0">
Imports <xmlns:im="http://niem.gov/niem/domains/immigration/2.0">
Imports <xmlns:lexslib="http://usdoj.gov/leisp/lexs/library/3.1">
Imports <xmlns:lexscodes="http://usdoj.gov/leisp/lexs/codes/3.1">

''' <summary>
''' LEXS IServiceCall Implementer
''' </summary>
''' <remarks></remarks>
Public Class LexsServiceCall
    Implements IServiceCall

#Region " Private Fields "
    Private _serviceCallDoc As XDocument
    Private _callDate As Date
    Private _reportNumber As String
    Private _callDescription As String
    Private _respondingOfficer As Officer
    Private _respondingOfficerXmlId As String
#End Region

#Region " Constructors "
    Public Sub New(ByVal serviceCall As XDocument)
        _serviceCallDoc = serviceCall
    End Sub
#End Region

#Region " IServiceCall Members "

    Public Property ReportNumber() As String Implements IServiceCall.ReportNumber
        Get
            If String.IsNullOrEmpty(ReportNumber) Then
                'lexspd:doPublish/lexs:publishMessage/lexs:DataItemPackage/lexs:Digest/lexsdigest:EntityActivity/nc:Activity/nc:ActivityIdentification/nc:IdentificationId
                _reportNumber = (From activity In _serviceCallDoc...<lexspd:doPublish> Select activity...<lexs:PublishMessage>...<lexs:DataItemPackage>...<lexs:Digest>...<lexsdigest:EntityActivity>...<nc:Activity>...<nc:ActivityIdentification>...<nc:IdentificationID>.Value).First
            End If

            Return _reportNumber
        End Get
        Set(ByVal value As String)
            _reportNumber = value
        End Set
    End Property

    Public Property RespondingOfficer() As Officer Implements IServiceCall.RespondingOfficer
        Get
            If _respondingOfficer Is Nothing Then
                'get the person id
                'lexspd:doPublish/lexs:publishMessage/lexs:DataItemPackage/lexs:Digest/j:ServiceCallDispatchedOfficial/nc:RoleOfPersonReference/@s:ref
                _respondingOfficerXmlId = (From people In _serviceCallDoc...<lexspd:doPublish> Select people...<lexs:PublishMessage>...<lexs:DataItemPackage>...<lexs:Digest>...<lexsdigest:EntityPerson>...<j:ServiceCallDispatchedOfficial>...<nc:RoleOfPersonReference>.@s:ref).First
                If Not String.IsNullOrEmpty(_respondingOfficerXmlId) Then
                    _respondingOfficer = New Officer

                    'Officer last name
                    _respondingOfficer.LastName = (From people In _serviceCallDoc...<lexspd:doPublish>...<lexs:PublishMessage>...<lexs:DataItemPackage>...<lexs:Digest>...<lexsdigest:EntityPerson> _
                                                   Where people...<lexsdigest:Person>.@s:id = _respondingOfficerXmlId _
                                                   Select people...<nc:PersonName>...<nc:PersonSurName>.Value).First

                    'Officer Dsn
                    _respondingOfficer.Dsn = (From people In _serviceCallDoc...<lexspd:doPublish> Select people...<lexs:PublishMessage>...<lexs:DataItemPackage>...<lexs:Digest>...<lexsdigest:EntityPerson>...<j:ServiceCallDispatchedOfficial>...<j:EnforcementOfficialBadgeIdentification>...<nc:IdentificationID>.Value).First
                End If
            End If

            Return _respondingOfficer
        End Get
        Set(ByVal value As Officer)
            _respondingOfficer = value
        End Set
    End Property

    Public Property CallDescription() As String Implements IServiceCall.CallDescription
        Get
            If String.IsNullOrEmpty(_callDescription) Then
                _callDescription = (From activity In _serviceCallDoc...<lexspd:doPublish> Select activity...<lexs:PublishMessage>...<lexs:DataItemPackage>...<lexs:Digest>...<lexsdigest:EntityActivity>...<nc:Activity>...<nc:ActivityDescriptionText>.Value).First
            End If

            Return _callDescription
        End Get
        Set(ByVal value As String)
            _callDescription = value
        End Set
    End Property

    Public Property CallDate() As Date Implements IServiceCall.CallDate
        Get
            If _callDate = Date.MinValue Then
                'lexspd:doPublish/lexs:publishMessage/lexs:DataItemPackage/lexs:Digest/lexsdigest:EntityActivity/nc:Activity/nc:ActivityDate/nc:DateTime
                _callDate = (From activity In _serviceCallDoc...<lexspd:doPublish> Select activity...<lexs:PublishMessage>...<lexs:DataItemPackage>...<lexs:Digest>...<lexsdigest:EntityActivity>...<nc:Activity>...<nc:ActivityDate>...<nc:DateTime>.Value).First
            End If

            Return _callDate
        End Get
        Set(ByVal value As Date)
            _callDate = value
        End Set
    End Property

#End Region

#Region " Build Document Members "
    ''' <summary>
    ''' Main method which builds the Service call Document
    ''' </summary>
    ''' <returns>A well-formed N-DEx incident arrest publish document</returns>
    ''' <remarks></remarks>
    Public Function BuildServiceCallDocument() As XDocument
        Dim serviceCallOutput As XDocument = <?xml version="1.0" encoding="UTF-8"?>
                                             <lexspd:doPublish
                                                 xmlns:nc="http://niem.gov/niem/niem-core/2.0"
                                                 xmlns:em="http://niem.gov/niem/domains/emergencyManagement/2.0"
                                                 xmlns:fips_10-4="http://niem.gov/niem/fips_10-4/1.0"
                                                 xmlns:i="http://niem.gov/niem/appinfo/2.0"
                                                 xmlns:j="http://niem.gov/niem/domains/jxdm/4.0"
                                                 xmlns:im="http://niem.gov/niem/domains/immigration/2.0"
                                                 xmlns:lexs="http://usdoj.gov/leisp/lexs/3.1"
                                                 xmlns:lexslib="http://usdoj.gov/leisp/lexs/library/3.1"
                                                 xmlns:lexscodes="http://usdoj.gov/leisp/lexs/codes/3.1"
                                                 xmlns:lexsdigest="http://usdoj.gov/leisp/lexs/digest/3.1"
                                                 xmlns:lexspd="http://usdoj.gov/leisp/lexs/publishdiscover/3.1"
                                                 xmlns:niem-xsd="http://niem.gov/niem/proxy/xsd/2.0"
                                                 xmlns:nonauth="http://niem.gov/niem/nonauthoritative-code/1.0"
                                                 xmlns:s="http://niem.gov/niem/structures/2.0"
                                                 xmlns:scr="http://niem.gov/niem/domains/screening/2.0"
                                                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                                                 <lexs:PublishMessageContainer>
                                                     <lexs:PublishMessage>
                                                         <%= GetPdMessageMetadata() %>
                                                         <%= GetDataSubmitterMetadata() %>
                                                         <lexs:DataItemPackage>
                                                             <%= GetPackageMetaData() %>
                                                             <lexs:Digest>
                                                                 <%= GetEntityActivity() %>
                                                                 <%= GetDispatchedOfficer() %>
                                                             </lexs:Digest>
                                                         </lexs:DataItemPackage>
                                                     </lexs:PublishMessage>
                                                 </lexs:PublishMessageContainer>
                                             </lexspd:doPublish>

        Return serviceCallOutput
    End Function

    ''' <summary>
    ''' Builds the PD Message Metadata element
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function GetPdMessageMetadata() As XElement
        Return <lexs:PDMessageMetadata>
                   <lexs:LEXSVersion>3.1.1</lexs:LEXSVersion>
                   <lexs:MessageDateTime><%= Now.ToUniversalTime %></lexs:MessageDateTime>
                   <lexs:MessageSequenceNumber><%= Now.Ticks.ToString %></lexs:MessageSequenceNumber>
                   <lexs:DataSensitivity>SBU</lexs:DataSensitivity>
               </lexs:PDMessageMetadata>
    End Function

    Private Shared Function GetPackageMetaData() As XElement
        Return <lexs:PackageMetadata>
                   <lexs:DataItemID>MISSPERS_00923482</lexs:DataItemID>
                   <lexs:DataItemContact>
                       <nc:PersonSurName>Smith</nc:PersonSurName>
                       <nc:PersonFullName>Mary Jones-Smith</nc:PersonFullName>
                       <nc:ContactTelephoneNumber>
                           <nc:FullTelephoneNumber>
                               <nc:TelephoneNumberFullID>333-444-5555</nc:TelephoneNumberFullID>
                               <nc:TelephoneSuffixID>6</nc:TelephoneSuffixID>
                           </nc:FullTelephoneNumber>
                       </nc:ContactTelephoneNumber>
                   </lexs:DataItemContact>
                   <lexs:DataItemDate>1967-08-13</lexs:DataItemDate>
                   <!-- DataItemReferenceID may be the same as the DataItemIDText, or may be a more human-readable version of DataItemID -->
                   <lexs:DataItemReferenceID>MISSPERS_00923482</lexs:DataItemReferenceID>
                   <lexs:DataItemPublishInstruction>Insert</lexs:DataItemPublishInstruction>
                   <lexs:DataItemStatus>Open</lexs:DataItemStatus>
                   <lexs:DataOwnerMetadata>
                       <lexs:DataOwnerIdentifier>
                           <lexs:ORI>GA1234567 GBI</lexs:ORI>
                           <nc:OrganizationName>Georgia Bureau of Investigation</nc:OrganizationName>
                           <lexs:SystemID>GIBRS</lexs:SystemID>
                       </lexs:DataOwnerIdentifier>
                       <lexs:DataOwnerContact>
                           <nc:PersonGivenName>James</nc:PersonGivenName>
                           <nc:PersonSurName>West</nc:PersonSurName>
                           <nc:ContactTelephoneNumber>
                               <nc:FullTelephoneNumber>
                                   <nc:TelephoneNumberFullID>123-555-1212</nc:TelephoneNumberFullID>
                               </nc:FullTelephoneNumber>
                           </nc:ContactTelephoneNumber>
                       </lexs:DataOwnerContact>
                   </lexs:DataOwnerMetadata>
                   <lexs:DisseminationCriteria>white</lexs:DisseminationCriteria>
                   <lexs:DataItemCategory>
                       <lexs:DataItemCategoryText>Missing Person Report</lexs:DataItemCategoryText>
                   </lexs:DataItemCategory>
               </lexs:PackageMetadata>
    End Function

    ''' <summary>
    ''' Builds the data submitter metadata
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function GetDataSubmitterMetadata() As XElement
        Return <lexs:DataSubmitterMetadata>
                   <lexs:SystemIdentifier>
                       <lexs:ORI>GA1234567 GBI</lexs:ORI>
                       <nc:OrganizationName>Georgia Bureau of Investigation</nc:OrganizationName>
                       <lexs:SystemID>GIBRS</lexs:SystemID>
                   </lexs:SystemIdentifier>

                   <lexs:SystemContact>
                       <nc:PersonGivenName>James</nc:PersonGivenName>
                       <nc:PersonSurName>West</nc:PersonSurName>
                       <nc:ContactTelephoneNumber>
                           <nc:NANPTelephoneNumber>
                               <nc:TelephoneAreaCodeID>123</nc:TelephoneAreaCodeID>
                               <nc:TelephoneExchangeID>555</nc:TelephoneExchangeID>
                               <nc:TelephoneLineID>1212</nc:TelephoneLineID>
                           </nc:NANPTelephoneNumber>
                       </nc:ContactTelephoneNumber>
                   </lexs:SystemContact>
               </lexs:DataSubmitterMetadata>
    End Function

    Private Function GetEntityActivity() As XElement
        Return <lexsdigest:EntityActivity>
                   <lexsdigest:Metadata s:id="MServiceCall1">
                       <nc:SourceIDText><%= ReportNumber %></nc:SourceIDText>
                   </lexsdigest:Metadata>

                   <nc:Activity s:id="ServiceCall1" s:metadata="MServiceCall1">
                       <nc:ActivityIdentification>
                           <!-- note that in this sample, the Service Call Number (ActivityIdentification/IdentificationID) is the same as the SourceIDText above  -->
                           <nc:IdentificationID><%= ReportNumber %></nc:IdentificationID>
                       </nc:ActivityIdentification>
                       <nc:ActivityCategoryText>Service Call</nc:ActivityCategoryText>
                       <nc:ActivityDate>
                           <nc:DateTime><%= CallDate.ToUniversalTime %></nc:DateTime>
                       </nc:ActivityDate>
                       <nc:ActivityDescriptionText><%= CallDescription %></nc:ActivityDescriptionText>
                   </nc:Activity>
               </lexsdigest:EntityActivity>
    End Function

    Private Function GetDispatchedOfficer() As XElement
        Return <lexsdigest:EntityPerson>
                   <lexsdigest:Metadata s:id="MPerson4">
                       <nc:SourceIDText>Person_4564567800005</nc:SourceIDText>
                   </lexsdigest:Metadata>

                   <lexsdigest:Metadata s:id="MOfficerPerson1">
                       <nc:SourceIDText>Officer_4564564</nc:SourceIDText>
                   </lexsdigest:Metadata>

                   <lexsdigest:Person s:id="Person4" s:metadata="MPerson4">
                       <nc:PersonName>
                           <nc:PersonSurName><%= RespondingOfficer.LastName %></nc:PersonSurName>
                       </nc:PersonName>
                   </lexsdigest:Person>

                   <j:ServiceCallDispatchedOfficial s:id="OfficerPerson1" s:metadata="MOfficerPerson1">
                       <nc:RoleOfPersonReference s:ref="Person4"/>
                       <j:EnforcementOfficialBadgeIdentification>
                           <nc:IdentificationID><%= RespondingOfficer.Dsn %></nc:IdentificationID>
                       </j:EnforcementOfficialBadgeIdentification>
                   </j:ServiceCallDispatchedOfficial>
               </lexsdigest:EntityPerson>
    End Function
#End Region

End Class
